<template>
  <div class="animated fadeIn">
    <div class="card card-accent-primary">
      <div class="card-header">
        <i class="fa fa-align-justify"></i> 添加规则
      </div>
      <div class="card-body">
        <Form ref="validate" label-position="left" :label-width="80" :model="formItems" :rules="rules">
          <div class="col-sm-6">
            <formItem label="规则名称:" prop="title">
              <Input type="text" v-model="formItems.title"></Input>
            </formItem>
            <formItem label="规则标识:" prop="name">
              <Input type="text" v-model="formItems.name"></Input>
            </formItem>
            <formItem>
              <i-button type="primary" @click="submit('validate')">提交</i-button>
              <i-button type="ghost" style="margin-left: 8px" @click="goback()">返回</i-button>
            </formItem>
          </div>
        </Form>
      </div>
    </div>
  </div>
</template>
<script>
import Mixin from 'common/js/mixin'

export default {
  data () {
    return {
      formItems: {
        name: '',
        title: ''
      },
      rules: {
        name: [
          {required: true, message: '请填写分组名称', trigger: 'blur'}
        ],
        title: [
          {required: true, message: '请填写规则名称', trigger: 'blur'}
        ]
      },
      authRule: []
    }
  },
  created () {
    this.getEdit()
  },
  methods: {
    getEdit: function () {
      this.$get('/api/admin/authRule/' + this.$route.params.id).then((res) => {
        if (res) {
          this.formItems.name = res.data.ar_name
          this.formItems.title = res.data.ar_title
        }
      })
    },
    submit (name) {
      this.$refs[name].validate((valid) => {
        if (valid) {
          this.$put('/api/admin/authRule/', this.$route.params.id, this.formItems).then((res) => {
            if (res) {
              this.$Message.success({
                content: res.mess,
                onClose: () => {
                  this.$router.push({name: 'authRuleIndex'})
                }
              })
            }
          })
        }
      })
    }
  },
  mixins: [Mixin]
}
</script>
